﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GonguklubburinnMVC.Models;

namespace GonguklubburinnMVC.Controllers
{
    public class MountainController : Controller
    {

        MountainRepository repo = new MountainRepository();
        
      
        //
        // GET: /Mountain/

      
        public ActionResult Index()
        {
		           
            var model = repo.showAll();
            return View(model);
        }



           [Authorize(Roles = "Admin")]
        public ActionResult IndexAdmin()
          {

        var model = repo.showAll();
            return View(model);

        }

        //
        // GET: /Mountain/Details/5

        public ActionResult Details(int id)
        {
            MountainE m = repo.GetMountain(id);
            if (m == null)
                return View("NotFound");
            return View(m);
        }

        //
        // GET: /Mountain/Create
    
        public ActionResult Create()
        {
			//List<string> YesOrNoList = new List<string>();
			//YesOrNoList.Add("No");
			//YesOrNoList.Add("Yes");
			//ViewData["ConfirmList"] = new SelectList(YesOrNoList);

            MountainE mountain = new MountainE()
            {
                InsertDate = DateTime.Now.AddDays(1)
            };
            return View(mountain);
        } 

        //
        // POST: /Mountain/Create

        [HttpPost] 
        public ActionResult Create( MountainE m )
        {
           // need to check if mountain exists in DB
           repo.AddMountain(m);
           repo.save();
           return RedirectToAction("Details", new  { id = m.ID });
        }
        
        //
        // GET: /Mountain/Edit/5
		
		[Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            MountainE mountain = repo.GetMountain(id);
            return View(mountain);            
                
        }

        //
        // POST: /Mountain/Edit/5

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id , FormCollection collection)
        {
			          
            var mntain2Update = repo.GetMountain(id);
            // Add validation Rules to mntain2Update 

            TryUpdateModel(mntain2Update, new String[] { "Name", "Height", "Hasgone", "ImgName", "InsertDate" , "TravelTime" , "HasGone"}, collection.ToValueProvider());
            if (ModelState.IsValid)
            {
				repo.save();
				return RedirectToAction("Index");
				
            }
            // Otherwise, reshow form
			return View(mntain2Update);
			   

     
        }

        //
        // GET: /Mountain/Delete/5
		[Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {


            MountainE mountain = repo.GetMountain(id);
            if (mountain == null)
                return View("NotFound");

            return View(mountain);
        }

        //
        // POST: /Mountain/Delete/5

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id,string confirmButton)
            {

                MountainE mountain = repo.GetMountain(id);

            if (mountain == null)
                return View("NotFound");



            repo.Delete(mountain);
            repo.save();

            return View("Deleted");
        }

	
    }
}
